/*
 * Project: BooEditor
 * Author:  Johannes Kuhaupt
 * Last Change:
 *    by:   $Author: $
 *    date: $Date:   $
 * Copyright (c): Johannes Kuhaupt
 * Licence: Creative Commons CC BY-NC-SA 3.0
 */
package control;

import java.io.File;
import java.io.IOException;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFileChooser;
import javax.swing.JOptionPane;
import view.MainFrame;
import view.CustomEditorPane;
import view.CustomTabPanel;
import view.fileFilter.LejosFilter;

/**
 *
 * @author Johannes Kuhaupt
 */
public class GuiManager {
    
    private static LinkedList<CustomEditorPane> allOpenTextEditor = new LinkedList<CustomEditorPane> ();
    private static java.util.ResourceBundle langBundle = java.util.ResourceBundle.getBundle("ressourceLanguage/Eg-Lejos-Editor");
    private static boolean bigScreenActive = false;
    private static CustomEditorPane activeEditorPane;
    
    
    public static CustomEditorPane getActivePane(){
        return activeEditorPane;
    }
    
    public static void setActivePane(CustomEditorPane pNewAndActivePane){
        activeEditorPane = pNewAndActivePane;
    }
    
    public static java.util.ResourceBundle getLangBundle(){
        return langBundle;
    }

    public static void addEditorToList(CustomEditorPane textEditor) {
        GuiManager.allOpenTextEditor.add(textEditor);
    }
    
    public static boolean bigScreenModus() {
        return bigScreenActive;
    }
    
    public static void setBigScreenModus(boolean pActive){
        GuiManager.bigScreenActive = pActive;
        
    }
    
    private MainFrame frame;
    JFileChooser fc;
    CustomEditorPane editor;
    CustomTabPanel tab;
    
    
    
    
    static GuiManager instance;
    public static GuiManager getInstance(){
        if(instance==null){
            instance = new GuiManager();
        }
        return instance;
    }
    
    /**
     *
     */
    private GuiManager(){
        this.frame = MainFrame.getInstance();
        fc = new JFileChooser();
        fc.setAcceptAllFileFilterUsed(false);
        fc.setFileFilter(new LejosFilter());
    }
    
    /**
     *
     */
    public void showGui(){
        this.frame.setVisible(true);
    }
    
    public void showOutputTab(){
        this.frame.showOutputTab();
    }
    
        
    /**
     *
     * @param pText
     */
    public void showMessage(String pText){
        JOptionPane.showMessageDialog(this.frame, pText, langBundle.getString("MsgInfoTitle") , JOptionPane.INFORMATION_MESSAGE);
    }
    
    /**
     *
     * @param pText
     */
    public void showErrorMessage(String pText){
        JOptionPane.showMessageDialog(this.frame, pText, langBundle.getString("MsgErrorTitle"), JOptionPane.ERROR_MESSAGE);
    }
    
    /**
     *
     * @param pNachricht
     * @param pTitel
     * @return
     */
    public boolean showYesNoMessage(String pNachricht,String pTitel){
        int n = JOptionPane.showConfirmDialog(this.frame, pNachricht, pTitel,JOptionPane.YES_NO_OPTION);
        if (n == JOptionPane.YES_OPTION){
            return true;
        }
        else{
            return false;
        }
    }
    
    
    /**
     *
     * @return
     */
    public File showOpenFileDialog(){
        int returnVal = fc.showOpenDialog(this.frame);          
        
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            File file = fc.getSelectedFile(); 
            return file;
        }
        else{
            return null;
        }
    }
    
    /**
     *
     * @return theFileLocation
     */
    public File showSaveFileDialog(){
        this.fc.setSelectedFile(new File("NewFile.java"));
        int returnVal = fc.showSaveDialog(this.frame);
        if (returnVal == JFileChooser.APPROVE_OPTION) {
            File file = fc.getSelectedFile();
            
            if(FileManager.hasJavaExtension(file)){
                return file;
            }
            else{
                
                try {
                    String newBooFile = file.getCanonicalPath()+".java";
                    fc.setSelectedFile(new File(newBooFile));                    
                    return fc.getSelectedFile();
                } catch (IOException ex) {
                    Logger.getLogger(GuiManager.class.getName()).log(Level.SEVERE, null, ex);
                    System.err.println("Error, showSaveFileDialog failed: "+ex.toString());
                    return file;
                }
            }
            
            
            
        }
        else{
            return null;
        }
    }
    
    public void updateGui(){
        //hier alle tabs updaten
        for ( Iterator<CustomEditorPane> i = GuiManager.allOpenTextEditor.iterator(); i.hasNext(); )
        {
            i.next().updateOptions();
            
        }

        this.frame.readSettingAndUpdateView();        
    }
    
   public void setInfoTextStatusBarExistingFile(String pFile){
        
            this.frame.setInfoTextStatusBar(langBundle.getString("File")+": "+pFile );
     
        
    }
   
   public void setInfoTextStatusBarNewFile(){
       this.frame.setInfoTextStatusBar(langBundle.getString("MsgInfoNewFile"));
   }
    
    /**
     *
     */
    public void setStandardFrameView(){
        this.frame.setStandardView();
    }
    
    public void setOutputInFrame(String pOutput){
        this.frame.setzeAusgabe(pOutput);
        this.frame.repaint();
    }
    
    public void setCompilerReady(boolean pReady){
        this.frame.setCompilerReady(pReady);
    }
    
    public void setUploaderReady(boolean pReady){
        this.frame.setUploaderReady(pReady);
    }

    
    public void openNewTabForNewFile(){
        this.tab = new CustomTabPanel(null);        
        this.frame.createNewTab(langBundle.getString("TitleNewFile"), this.tab);
        this.frame.setInfoTextStatusBar(langBundle.getString("MsgInfoNewFile"));
        this.tab.setFocusToEditor();        
    }
    
    public void openNewTabForExistingFile(File pFile){
        this.tab = new CustomTabPanel(pFile);
        this.frame.createNewTab(pFile.getName(),this.tab );
        try {
            this.tab.loadFileInEditor(pFile);
        } catch (IOException ex) {
            this.showErrorMessage(langBundle.getString("MsgErrorFileOpening")+"\n"+ex.toString());
            Logger.getLogger(GuiManager.class.getName()).log(Level.SEVERE, null, ex);
        }
        
    }

    void setActiveTabHeader(String name) {
        this.frame.setTabTitle(name);
    }
    
    public void startGui(){        
            this.updateGui();
            this.showGui();   
        this.openNewTabForNewFile();
        
    }

    public void setStatusInfo(String pInfoText) {
        this.frame.setStatusInfo(pInfoText);
    }
    
    public void repaintAndUpdateGui(){
        //this.frame.pack();
        //this.frame.repaint();
        //this.frame.pack();
        this.frame.validate();
    }

}
